package cn.com.gridinfo.sooc.core.expand.callname.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremy.arad.utils.Log;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.expand.callname.adapter.QueXiAdapter;
import cn.com.gridinfo.sooc.core.expand.callname.adapter.QueXiTongAdapter;
import cn.com.gridinfo.sooc.core.expand.callname.common.CodeConst;
import cn.com.gridinfo.sooc.core.expand.callname.common.PageConst;
import cn.com.gridinfo.sooc.core.expand.callname.dao.CallNameDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 缺席统计界面
 * Created by yingchunli on 2016/1/13.
 */
public class QuexiTongjiActivity extends BaseSoocActivity {

    CallNameDao dao;
    @InjectView(R.id.kcmc_quexitongji)
    TextView kcmc;//课程名称
    @InjectView(R.id.skxq_quexitongji)
    TextView skxq;//上课学期
    @InjectView(R.id.qxrs_quexitongji)
    TextView qxrs;//缺席人数
    @InjectView(R.id.dmrs_quexitongji)
    TextView dmcs;//点名次数
    @InjectView(R.id.list_quexitongji)
    UltimateRecyclerView listQuexi;//列表
    LinearLayoutManager linearLayoutManager;//布局管理器
    QueXiTongAdapter adapter;//适配器
    int maxLastPositon;
    int pageNo = 1;
    String kcid;//课程id

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expand_callname_quexitongji);
        ButterKnife.inject(this);
        dao = new CallNameDao(this);
        linearLayoutManager = new LinearLayoutManager(this);
        listQuexi.setLayoutManager(linearLayoutManager);

        kcmc.setText(getIntent().getStringExtra("kcmc"));//设置课程名称
        skxq.setText(getIntent().getStringExtra("skxq"));//设置上课学期
        qxrs.setText("");
        dmcs.setText(getIntent().getStringExtra("dmcs"));//设置点名次数
        kcid = getIntent().getStringExtra("kcid");
        PageConst.pageSize = 10;
        //发送请求  获取列表数据
        dao.getquexiStudent(kcid, pageNo, PageConst.pageSize, CodeConst.EXPAND_QUQXI_GET_STUDENT_FRIST);

    }

    /**
     * 初始化界面数据
     */
    private void initView() {
        adapter = new QueXiTongAdapter(dao.getStudentList(), this, kcid, getIntent().getStringExtra("kcmc"));
        listQuexi.setAdapter(adapter);
        listQuexi.enableLoadmore();
        listQuexi.setDefaultOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageNo = 1;
                dao.getquexiStudent(kcid,pageNo, PageConst.pageSize, CodeConst.EXPAND_QUQXI_GET_STUDENT_PUSHDOWN);
                linearLayoutManager.scrollToPosition(0);
            }
        });
        listQuexi.setOnLoadMoreListener(new UltimateRecyclerView.OnLoadMoreListener() {
            @Override
            public void loadMore(final int itemsCount, final int maxLastVisiblePosition) {
                if (itemsCount > (PageConst.pageSize - 1)) {
                    maxLastPositon = maxLastVisiblePosition;
                dao.getquexiStudent(kcid, ++pageNo, PageConst.pageSize, CodeConst.EXPAND_QUQXI_GET_STUDENT_PULLUP);
                } else {
                     adapter.loadNoDataView();
                linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                }
            }
        });
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            if (dao.getStatus().equals("0")) {
                switch (requestCode) {
                    case CodeConst.EXPAND_QUQXI_GET_STUDENT_FRIST://获取列表  首次请求
                        initView();
                        qxrs.setText(dao.getStudentCount() + "");//设置缺席人数
                        break;
                    case CodeConst.EXPAND_QUQXI_GET_STUDENT_PUSHDOWN://获取列表
                        listQuexi.setRefreshing(false);
                        if (dao.getStudentList() != null && !dao.getStudentList().isEmpty()) {
                            qxrs.setText(dao.getStudentCount() + "");//设置缺席人数

                            listQuexi.hideEmptyView();
                        } else {
                            listQuexi.showEmptyView();//无数据处理
                        }
                        adapter.refresh(dao.getStudentList());
                        if (dao.getStudentList().size() > PageConst.pageSize - 1) {
                            adapter.loadMoreDataView();
                        } else {
                            //数据小于当前页的话  不显示加载更多
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                            linearLayoutManager.scrollToPosition(maxLastPositon);
                        }
                        break;
                    case CodeConst.EXPAND_QUQXI_GET_STUDENT_PULLUP://获取列表
                        if (dao.getStudentList() != null && !dao.getStudentList().isEmpty()) {
                            adapter.loadmore(dao.getStudentList());
                        } else {
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                        }
                        break;
                }
            } else {
                MessageUtils.showImageShortToast(this, dao.getStatusCode());
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(this, "请求数据异常");
        }
    }


    @Override
    public String setupToolBarTitle() {
        return "缺席统计";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    @OnClick(R.id.toolbar_leftbtn)
    void onToolbarClick() {
        finish();
    }
}
